package com.laputacastle.dr.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 常用方法集合
 * 
 * @author Administrator
 * 
 */
public final class CommonUtils {
    private CommonUtils() {}

    /**
     * 生成uuid
     * @return
     */
    public static final String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 集合分组,如果groupBy返回的key为空不参与分组
     * @param colls
     * @param gb
     * @return
     */
    public static final <T extends Comparable<T>, D> Map<T, List<D>> group(
            Collection<D> colls, GroupBy<T> gb) {
        Map<T, List<D>> map = new HashMap<T, List<D>>();
        if (colls == null || colls.isEmpty()) {
            return map;
        }
        if (gb == null) {
            return map;
        }
        Iterator<D> iter = colls.iterator();
        while (iter.hasNext()) {
            D d = iter.next();
            T t = gb.groupby(d);
            if (t == null) {
                continue;
            }
            if (map.containsKey(t)) {
                map.get(t).add(d);
            } else {
                List<D> list = new ArrayList<D>();
                list.add(d);
                map.put(t, list);
            }
        }
        return map;
    }

    /**
     * 根据指定字段去重
     * @param list
     * @param gb
     * @return
     */
    public static final <D extends Comparable<D>, T> List<T> removeDuplicate(
            List<T> list, final GroupBy<D> gb) {
        TreeSet<T> set = new TreeSet<T>(new Comparator<T>() {
            @Override
            public int compare(T o1, T o2) {
                D d1 = gb.groupby(o1);
                D d2 = gb.groupby(o2);
                return d1.compareTo(d2);
            }
        });
        set.addAll(list);
        return new ArrayList<T>(set);
    }
    
    /**
     * 根据身份证号获取年龄
     * @param IdNO 身份证号
     * @return
     */
    public static String idNoToAge(String IdNO){
        int leh = IdNO.length();
        String ageStr = null;
        if (leh == 18) {
            String dates = IdNO.substring(6, 10);
            SimpleDateFormat df = new SimpleDateFormat("yyyy");
            String year=df.format(new Date());
            int age = Integer.parseInt(year) - Integer.parseInt(dates);
            if(age > 0){
                ageStr = String.valueOf(age);
            }
        }
        return ageStr;
    }
    
    /**
     * 根据身份证号和起诉日期获取当事人起诉年龄
     * @param IdNO 身份证号
     * @param date 相关日期
     * @return
     */
    public static String idNoAndXgrqToAge(String IdNO, Date date){
        int leh = IdNO.length();
        String ageStr = null;
        if (leh == 18) {
            String dates = IdNO.substring(6, 10);
            SimpleDateFormat df = new SimpleDateFormat("yyyy");
            String year=df.format(date);
            if (isStr2Num(year) && isStr2Num(dates)) {
                int age = Integer.parseInt(year) - Integer.parseInt(dates);
                if(age > 0){
                    ageStr = String.valueOf(age);
                }
            }
        }
        return ageStr;
    }
    
    /**
     * 拼接字符串
     * @param str 身份证号
     * @param bstr 相关日期
     * @return
     */
    public static String conCatenatedString(String str, String bstr){
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append(str);
        strBuilder.append(bstr);
        return strBuilder.toString();
    }

    /**
     * 查看一个字符串是否可以转换为数字
     * @param str 字符串
     * @return true 可以; false 不可以
     */
    public static boolean isStr2Num(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 匹配正则
     * @param patternName 正则表达式
     * @param text 文书内容
     * @return 匹配正则Matcher
     */
    public static Matcher match(String patternName, String text) {
        Pattern patter = Pattern.compile(patternName);
        Matcher matcher = patter.matcher(text);
        return matcher;
    }

    /**
     * 中文數字转阿拉伯数字
     * @param chineseNumber
     * @return
     */
    public static String chineseNumber2Int(String chineseNumber){
        int result = 0;
        if (StringUtils.isBlank(chineseNumber)) {
            return null;
        }
        // 存放一个单位的数字如：十万
        int temp = 1;
        // 判断是否有chArr
        int count = 0;
        char[] cnArr = new char[]{'一','二','三','四','五','六','七','八','九'};
        char[] chArr = new char[]{'十','百','千','万','亿'};
        for (int i = 0; i < chineseNumber.length(); i++) {
            // 判断是否是chArr
            boolean b = true;
            char c = chineseNumber.charAt(i);
            // 非单位，即数字
            for (int j = 0; j < cnArr.length; j++) {
                if (c == cnArr[j]) {
                    // 添加下一个单位之前，先把上一个单位值添加到结果中
                    if(0 != count){
                        result += temp;
                        temp = 1;
                        count = 0;
                    }
                    // 下标+1，就是对应的值
                    temp = j + 1;
                    b = false;
                    break;
                }
            }
            // 单位{'十','百','千','万','亿'}
            if(b){
                for (int j = 0; j < chArr.length; j++) {
                    if (c == chArr[j]) {
                        switch (j) {
                            case 0:
                                temp *= 10;
                                break;
                            case 1:
                                temp *= 100;
                                break;
                            case 2:
                                temp *= 1000;
                                break;
                            case 3:
                                temp *= 10000;
                                break;
                            case 4:
                                temp *= 100000000;
                                break;
                            default:
                                break;
                        }
                        count++;
                    }
                }
            }
            // 遍历到最后一个字符
            if (i == chineseNumber.length() - 1) {
                result += temp;
            }
        }
        return Integer.toString(result);
    }
    /**
     * 根据数字获取对应英文字母
     * @param number
     * @return
     */
    public static String numberToEnglish(String number){
        int n = Integer.parseInt(number) - 1;
        String[] enArr = new String[]{"a","b","c","d","e","f","g","h","i","j"};
        if (n < enArr.length) {
            return enArr[n];
        }
        return null;
    }

    public static String removeProvOrCityOrArea (String str) {
        return StringUtils.isNotBlank(str) ? str
                .replace("省", "")
                .replace("市", "")
                .replace("区", "")
                .replace("县", "")
                .replace("人民", "") : null;
    }

}
